package testcases

import auth.Constants
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.context.request.RequestContextHolder

@RestController
@RequestMapping("/hello")
class HelloController {

    @PreAuthorize("#oauth2.hasScope('read')")
    @GetMapping("/hi/{name}")
    String sayHi(@PathVariable("name") String name) {
        def requestAttributes = RequestContextHolder.getRequestAttributes()
        def realName = requestAttributes.getAttribute(Constants.JWT_USER_REAL_NAME, 0)
        "${realName}: hello, ${name ?: 'world'}"
    }
}
